package com.yili.oa.service.impl;

import com.yili.oa.dao.AllGameMapper;
import com.yili.oa.dao.FtGameKindMapper;
import com.yili.oa.dao.FtGamePlatformMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.*;
import com.yili.oa.service.GameService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.request.GameDataReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/19 0019
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Service
@DataBase("lyt_game_db")
@Transactional(value = "transactionManager")
public class GameSvImpl implements GameService {

    @Autowired
    private AllGameMapper mapper;

    @Autowired
    private FtGameKindMapper ftGameKindMapper;

    @Autowired
    private FtGamePlatformMapper platformMapper;

    @Override
    public List<AllGame> getList(SearchBase searchBase) {
        AllGameExample example = new AllGameExample();
        AllGameExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(searchBase.getId())){
            Long id = 0L;
            try{
                id = Long.valueOf(searchBase.getId());
            }catch (Exception e){
                throw new AjaxException("id格式不对");
            }
            criteria.andGameIdEqualTo(id);
        }
        example.setOrderByClause("game_kind ASC,game_platform ASC,game_create_time ASC");
        return mapper.selectByExample(example);
    }

    @Override
    public void add(GameDataReq gameDataReq) {
        AllGame allGame = new AllGame();
        allGame.setGameName(gameDataReq.getGameName());
        allGame.setGameXima(gameDataReq.getGameXima());
        allGame.setGameKind(gameDataReq.getGameKind());
        allGame.setGamePlatform(gameDataReq.getGamePlatform());
        allGame.setGameCreateTime(new Date());
        allGame.setGameStatus(0);
        try {
            mapper.insert(allGame);
        }catch (Exception e){
            throw new AjaxException("新增失败");
        }
    }

    @Override
    public void update(GameDataReq gameDataReq) {
         AllGame allGame = mapper.selectByPrimaryKey(gameDataReq.getGameId());
         if (allGame==null){
             throw new AjaxException("该游戏不存在");
         }
         if (StringUtils.isNotBlank(gameDataReq.getGameName())) {
             allGame.setGameName(gameDataReq.getGameName());
         }
        if (gameDataReq.getGameStatus()!=null) {
            allGame.setGameStatus(gameDataReq.getGameStatus());
        }
         if (gameDataReq.getGameXima()!=null){
             allGame.setGameXima(gameDataReq.getGameXima());
         }
         mapper.updateByPrimaryKeySelective(allGame);
    }

    @Override
    public boolean checkName(String gameName, Long gameId) {
        AllGameExample example = new AllGameExample();
        AllGameExample.Criteria criteria = example.createCriteria();
        criteria.andGameNameEqualTo(gameName);
        if (gameId!=null){
            criteria.andGameIdNotEqualTo(gameId);
        }
        List<AllGame> list = mapper.selectByExample(example);
        AllGame allGame = new AllGame();
        if (ListUtils.isNotNullOrEmpty(list)){
            allGame = list.get(0);
            return false;
        }
        return true;
    }

    @Override
    public void delGame(List<Long> ids) {
        AllGameExample example = new AllGameExample();
        AllGameExample.Criteria criteria = example.createCriteria();
        criteria.andGameIdIn(ids);
        try{
            mapper.deleteByExample(example);
        }catch (Exception e){
            throw new AjaxException("删除失败");
        }
    }

    @Override
    public List<FtGameKind> getGameKind() {
        FtGameKindExample example = new FtGameKindExample();
        return ftGameKindMapper.selectByExample(example);
    }

    @Override
    public List<FtGamePlatform> getPlatform(long id) {
        FtGamePlatformExample example = new FtGamePlatformExample();
        FtGamePlatformExample.Criteria criteria = example.createCriteria();
        criteria.andGameKindEqualTo(id);
        return platformMapper.selectByExample(example);
    }

    @Override
    public List<AllGame> getGame(long id) {
        AllGameExample example = new AllGameExample();
        AllGameExample.Criteria criteria = example.createCriteria();
        criteria.andGameKindEqualTo(id);
        return mapper.selectByExample(example);
    }
}
